package ejb.facades;

import entities.Trip;
import entities.User;
import entities.Vehicle;
import entities.location.Address;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Trabalho PD - 2014/2015
 *
 * @author a21210380 a21210392
 */
@Stateless
public class TripFacade extends AbstractFacade<Trip> implements TripFacadeLocal {

    @PersistenceContext(unitName = "Trabalho_PDPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TripFacade() {
        super(Trip.class);
    }
    
    @Override
    public List<Trip> findUnfinishedTrips() {
        return em.createNamedQuery("Trip.findUnfinishedTrips", Trip.class)
                .getResultList();
    }

    @Override
    public List<Trip> findAllTripsCreatedBy(User user) {
        return em.createNamedQuery("Trip.findAllTripsCreatedBy", Trip.class)
                .setParameter(1, user.getId())
                .getResultList();
    }

    @Override
    public List<Trip> findByVehicle(Vehicle vehicle) {
        return em.createNamedQuery("Trip.findByVehicleId", Trip.class)
                .setParameter(1, vehicle.getId())
                .getResultList();
    }

    @Override
    public List<Trip> findRecent(int top) {
        return em.createNamedQuery("Trip.findRecent", Trip.class)
                .setMaxResults(top)
                .getResultList();
    }

    @Override
    public List<Trip> searchTrips(Address start, Date date) {
        if (date != null) {
            return em.createNamedQuery("Trip.searchByStartingPlaceAndDate", Trip.class)
                    .setParameter(1, start.getId())
                    .setParameter(2, date)
                    .getResultList();
        }
        return em.createNamedQuery("Trip.searchByStartingPlace", Trip.class)
                .setParameter(1, start.getId())
                .getResultList();
    }

    @Override
    public List<Trip> searchTrips(Address end) {
        return em.createNamedQuery("Trip.searchByEndingPlace", Trip.class)
                .setParameter(1, end.getId())
                .getResultList();
    }

    @Override
    public List<Trip> searchTrips(Date date) {
        return em.createNamedQuery("Trip.searchByEstimatedDepartureDate", Trip.class)
                .setParameter(1, date)
                .getResultList();
    }

    @Override
    public List<Trip> searchTrips(Address start, Address end, Date date) {
        if (date != null) {
            return em.createNamedQuery("Trip.searchByStartingAndEndingPlaceAndDate", Trip.class)
                    .setParameter(1, end.getId())
                    .setParameter(2, start.getId())
                    .setParameter(3, date)
                    .getResultList();
        }
        return em.createNamedQuery("Trip.searchByStartingAndEndingPlace", Trip.class)
                .setParameter(1, end.getId())
                .setParameter(2, start.getId())
                .getResultList();
    }
}
